import cooApp, {
  ui,
  Widget,
  basicWidgets,
  cooInfo,
  FileManager
} from 'coolink-ts'
import { Colors } from '../config'
import func, { px } from '../base/func'
import { showAutoHideToast } from '../widgets/Toast'


const {
  Page,
  AppBar,
  Container,
  Text,
} = basicWidgets

export default class HomePage extends Widget<any, any> {

  state = {
    filePath:"/data/user/0/com.coocaa.link/cache/file_picker/小酷互联TV端工程.pdf",//手机端默认路径

    // 界面样式
    backgroundGradient: {
      colors: [0xFF9C04D0, 0xFFFF60D5],
      type: 'linear',
      begin: 'centerLeft',
      end: 'centerRight'
    },

    boxShadow: {
      color: Colors.Black,
      blurRadius: 24,
      spreadRadius: 0,
    },
  }

  openFile(){
    let filePath = this.state.filePath
    if(filePath) {
      if(!filePath.endsWith(".pdf")) {
        // var suffix = filePath.substring(filePath.lastIndexOf('.')+1)
        // if(suffix == "ppt" || suffix == "pptx" || suffix == "doc" || suffix == "docx" || suffix == "xls" || suffix == "xlsx"){
            //使用tbs预览？？？
        // }
        showAutoHideToast("抱歉, 目前只支持PDF文件")
        return
      }
      var fileName = filePath.substring(filePath.lastIndexOf('/')+1).replace(".pdf","");
      cooApp.pushPage("pdfPreviewPage",{filePath,fileName})
    } else {
      showAutoHideToast("请先选择文件")
    }
  }

  chooseLocalFile(){
    // this.setState({
    //   filePath:""
    // })
    FileManager.pickFiles(this.onFilePickerCallBack.bind(this));
  }

  onFilePickerCallBack(res){
    this.setState({
      filePath:res
    })
  }

  isTV() {
    if (cooInfo.infos?.systemInfo?.os && (cooInfo.infos.systemInfo.os.indexOf('ccos') >= 0)) {
        return true;
    }
    return false;
  }

render() {
  const appbarHeight = ui.appbarHeight;
  const statusBarHeight = Math.round(ui.statusBarHeight);
  const bottomBarHeight = ui.bottomBarHeight;
  const clientHeight = ui.screenHeight - statusBarHeight - appbarHeight - bottomBarHeight;
  return (
      this.isTV() ?
      //tv端
      <Page>
        <Container relative={true} width={ui.screenWidth} height={ui.screenHeight} justifyContent={"center"} alignItems={'center'}>
            <Text margin={5} weight={'bolder'} color={Colors.Black} >{'Empty Page(TV端不需要主页): '}</Text>
        </Container>
      </Page>
      :
      //手机端
      <Page appBar={<AppBar title={'PDF阅读器'} backgroundColor={Colors.White} elevation />}>
        <Container relative={true} width={ui.screenWidth} height={clientHeight}>
          <Container flex={1} relative={true} absolute={{ top: px(40), left: 10 }} height={ui.screenHeight} width={ui.screenWidth}>
            <Text richText={[{ text: "【"+this.state.filePath+"】", color: 0xff333333 }]} margin={10} weight={'bolder'} color={Colors.Black} >{'当前选择的文件路径:  '}</Text>
            <Text backgroundColor={Colors.Blue} padding={12} margin={8} weight={'bolder'} color={Colors.White}
                  onTap={this.chooseLocalFile.bind(this)} >{'选择本地文件'}</Text>
            <Text backgroundColor={Colors.Blue} padding={12} margin={8} weight={'bolder'} color={Colors.White}
                onTap={this.openFile.bind(this)} >{'打开当前选择的文件'}</Text>
          </Container>
          </Container>
      </Page>
    )
  }
}


